package com.cookie.framework.bmob;

import android.content.Context;

import com.cookie.framework.utils.CommonUtils;

import java.io.File;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FetchUserInfoListener;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.framework.manager
 * 文件名:    BmobManger
 * 创建者:    cookie
 * 创建时间:   2020/1/1 0:12
 * 描述:      Bmob管理类
 */

public class BmobManger {
    //f8efae5debf319071b44339cf51153fc
    //529f95dbbf5bbef72b52d0d204a1f95d 自己的
    private static final String BMOB_SDK_ID = "529f95dbbf5bbef72b52d0d204a1f95d";
    private volatile static BmobManger instance = null;

    private BmobManger() {
    }

    public static BmobManger getInstance() {
        if (instance == null) {
            synchronized (BmobManger.class) {
                if (instance == null) {
                    instance = new BmobManger();
                }
            }
        }
        return instance;
    }

    //初始化bmob
    public void initBomb(Context context) {
        Bmob.initialize(context, BMOB_SDK_ID);
    }

    public boolean isLogin() {
        return BmobUser.isLogin();
    }

    //获取本地对象
    public IMUser getUser() {
        return BmobUser.getCurrentUser(IMUser.class);
    }

    /**
     * 同步控制台信息至本地缓存
     */
    public void fetchUserInfo(FetchUserInfoListener<BmobUser> listener) {
        BmobUser.fetchUserInfo(listener);
    }

    //发送短信验证码
    public void requestSMS(String phone, QueryListener<Integer> listener) {
        BmobSMS.requestSMSCode(phone, "", listener);
    }

    //通过手机号码注册或者登陆
    public void signOrLoginByMobilePhone(String phone, String code, LogInListener<IMUser> listener) {
        BmobUser.signOrLoginByMobilePhone(phone, code, listener);
    }

    public void uploadFirstPhoto(final String nickName, File file, final OnUploadPhotoListener listener) {
        //1.上传文件拿到地址 2.更新用户信息
        final IMUser user = getUser();
        final BmobFile bmobFile = new BmobFile(file);
        bmobFile.uploadblock(new UploadFileListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    //上传成功
                    user.setNickName(nickName);
                    user.setPhoto(bmobFile.getFileUrl());
                    user.setTokenNickName(nickName);
                    user.setTokenPhoto(bmobFile.getFileUrl());
                    //更新用户信息
                    user.update(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                listener.onUpdateDone();
                            } else {
                                listener.onUpdateFail(e);
                            }
                        }
                    });
                } else {
                    listener.onUpdateFail(e);
                }
            }
        });
    }

    public interface OnUploadPhotoListener {
        void onUpdateDone();

        void onUpdateFail(BmobException e);
    }

    //根据电话号码查询用户
    public void queryPhoneUser(String phone, FindListener<IMUser> listener) {
        baseQuery("mobilePhoneNumber", phone, listener);
    }

    //根据电话号码查询用户 mobilePhoneNumber
    public void queryPhoneFriend(String phone, FindListener<IMUser> listener) {
        baseQuery("mobilePhoneNumber", phone, listener);
    }

    //根据objectId查询用户
    public void queryObjectIdUser(String objectId, FindListener<IMUser> listener) {
        baseQuery("objectId", objectId, listener);
    }

    //查询所有用户
    public void queryAllUser(FindListener<IMUser> listener) {
        BmobQuery<IMUser> query = new BmobQuery<>();
        query.findObjects(listener);
    }

    //查询所有广场
    public void queryAllSquare(FindListener<SquareSet> listener) {
        BmobQuery<SquareSet> query = new BmobQuery<>();
        query.findObjects(listener);
    }

    //查询缘分池
    public void queryFateSet(FindListener<FateSet> listener) {
        BmobQuery<FateSet> query = new BmobQuery<>();
        query.findObjects(listener);
    }

    //查询我的好友
    public void queryMyFriends(FindListener<Friend> listener) {
        BmobQuery<Friend> query = new BmobQuery<>();
        query.addWhereEqualTo("user", getUser());
        query.findObjects(listener);
    }

    //查询基类
    private void baseQuery(String key, String values, FindListener<IMUser> listener) {
        BmobQuery<IMUser> query = new BmobQuery<>();
        query.addWhereEqualTo(key, values);
        query.findObjects(listener);
    }

    //添加好友
    public void addFriend(IMUser imUser, SaveListener<String> listener) {
        Friend friend = new Friend();
        friend.setUser(getUser());
        friend.setFriendUser(imUser);
        friend.save(listener);
    }

    //添加到缘分池中
    public void addFateSet(SaveListener<String> listener) {
        FateSet set = new FateSet();
        set.setUserId(getUser().getObjectId());
        set.save(listener);
    }

    //添加私有库
    public void addPrivateSet(SaveListener<String> listener) {
        PrivateSet set = new PrivateSet();
        set.setUserId(getUser().getObjectId());
        set.setPhone(getUser().getMobilePhoneNumber());
        set.save(listener);
    }

    //删除私有库
    public void delPrivateSet(String id, UpdateListener listener) {
        PrivateSet set = new PrivateSet();
        set.setObjectId(id);
        set.delete(listener);
    }

    //发布广场
    public void pushSquare(int mediaType, String text, String path,SaveListener<String> listener) {
        SquareSet squareSet = new SquareSet();
        squareSet.setUserId(getUser().getObjectId());
        squareSet.setPushTime(System.currentTimeMillis());
        squareSet.setText(text);
        squareSet.setPushType(mediaType);
        squareSet.setMediaUrl(path);

        squareSet.save(listener);
    }

    //删除缘分池
    public void delFateSet(String id, UpdateListener listener) {
        FateSet set = new FateSet();
        set.setObjectId(id);
        set.delete(listener);
    }

    //通过ID添加好友
    public void addFriend(String id, final SaveListener<String> listener) {
        queryObjectIdUser(id, new FindListener<IMUser>() {
            @Override
            public void done(List<IMUser> list, BmobException e) {
                if (e == null) {
                    if (CommonUtils.isEmpty(list)) {
                        IMUser imUser = list.get(0);
                        addFriend(imUser, listener);
                    }
                }
            }
        });
    }

    //查询私有库
    public void queryPrivateSet(FindListener<PrivateSet> listener) {
        BmobQuery<PrivateSet> query = new BmobQuery<>();
        query.findObjects(listener);
    }
}
